package com.base.component.ai.agent.core;

import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.base.component.ai.agent.bo.ContextBO;
import com.base.component.ai.agent.bo.ContextParamBO;
import com.base.component.ai.agent.core.detection.ContentDetection;
import com.base.component.ai.agent.core.enums.DetectionTypeEnum;
import com.base.component.ai.agent.core.recognition.ContentRecognition;

@Service
public class ContentTemplate<MSG,AP extends ContextParamBO,PP> {
	
	@Autowired(required = false)
	private List<ContentRecognition<MSG>> contentRecognitions;
	@Autowired(required = false)
	private List<ContentDetection<MSG>> detectionHandlers;
	@Autowired(required = false)
	private List<AgentProxyService<MSG,AP,PP>> chatProxyServices;
	
	public void contentRecognition(	MSG msg) {
		for(ContentRecognition<MSG> ca:contentRecognitions) {
			if(ca.support(msg.getClass())) {
				ca.execute(msg);
			}
		}
	}
	
	/**
	 * 风控检测
	 * @param scene
	 * @param content
	 * @param contentType
	 * @return
	 */
	public DetectionTypeEnum contentDetection(String scene,MSG content) {
		if(CollectionUtils.isEmpty(detectionHandlers)) {
			return DetectionTypeEnum.OK;
		}
		for(ContentDetection<MSG> handler:detectionHandlers) {
			if(handler.support(content.getClass())) {
				DetectionTypeEnum riskType=handler.check(scene,content);
				if(riskType!=DetectionTypeEnum.OK) {
					return riskType;
				}
			}
		}
		return DetectionTypeEnum.OK;
	}
	
	public AgentProxyService<MSG,AP,PP> getProxy(String name) {
		for(AgentProxyService<MSG,AP,PP> handler:chatProxyServices) {
			if(handler.support(name)) {
				return handler;
			}
		}
//		throw new ApplicationException("找不到执行ChatProxyService:"+name);
		//执行默认Proxy
		return new AgentProxyService<MSG, AP, PP>() {

			@Override
			public boolean support(String name) {
				return true;
			}

			@Override
			public boolean afterPropertiesSet(ContextBO<MSG, AP, PP> context, Consumer<Object> ws) {
				return true;
			}

			@Override
			public <M> boolean msgHandle(ContextBO<MSG, AP, PP> context, M item) {
				return true;
			}

			@Override
			public void destory(ContextBO<MSG, AP, PP> context) {
			}
			
		};
	}
	
}
